<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/19
 * Time: 21:05
 */
namespace app\models\form;

use app\models\UserPet;
use Yii;
use yii\data\Sort;use yii\helpers\Html;

class PetSearchForm
{
    protected $userPet;

    protected $attributes;

    protected $_query;


    public function __construct()
    {
        $this->userPet = new UserPet();
    }

    /**
     * @param array $attributes
     */
    public function setAttributes(array $attributes)
    {
        $this->attribtues = $attributes;
    }

    /**
     * @param $name
     * @return string
     */
    public function getAttribute($name)
    {
        return isset($this->attribtues[$name]) ? $this->attribtues[$name] : '';
    }

    /**
     * @param $query
     */
    public function loadSearch($query)
    {
        $this->_query = $query;
        $getData = Yii::$app->request->get();
        if (!empty($getData)) {
            $this->setAttributes($getData);
        }
        $postData = Yii::$app->request->post();
        if (!empty($postData)) {
            $this->setAttributes($postData);
        }


        return $this->loadData();
    }

    /**
     * @return mixed
     */
    protected function loadData()
    {
        $growMax = $this->getAttribute('grow_max');
        $growMin = $this->getAttribute('grow_min');

        $nuggetsMax = $this->getAttribute('nuggets_max');
        $nuggetsMin = $this->getAttribute('nuggets_min');

        $heightMin = $this->getAttribute('height_min');
        $heightMax = $this->getAttribute('height_max');
        $algebra = $this->getAttribute('select-gen');
        if (!empty($algebra)) {
            $this->_query->andWhere('pet.algebra=:algebra', [':algebra' => $algebra]);
        }

        $this->getBetweenQuery('pet.grow', $growMin, $growMax);
        $this->getBetweenQuery('pet.nuggets', $nuggetsMin, $nuggetsMax);
        $this->getBetweenQuery('pet.height', $heightMin, $heightMax);

        $this->getSortBy();

        return $this->_query;
    }

    /**
     * 设置排序
     * @return mixed
     */
    protected function getSortBy()
    {
        $order = Yii::$app->request->get('order');
        $sortAttribute = [];
        if (!empty($order)) {
            $orderParams = $this->getSortParams();
            $sort = $orderParams['sort'] == '-' ? SORT_ASC : SORT_DESC;
            $sortAttribute = [
                'pet.'.$orderParams['order'] => $sort
            ];
        }
        $this->_query->orderBy(array_merge($sortAttribute, [
            'user_pet.puid' => SORT_DESC
        ]));
        return $this->_query;
    }

    /**
     * @param $column
     * @param $min
     * @param $max
     * @return mixed
     */
    protected function getBetweenQuery($column, $min, $max)
    {
        if (empty($min) || empty($max)) {
            return $this->_query;
        }
        $minkey = $this->getColumnName($column, 'min');
        $maxkey = $this->getColumnName($column, 'max');
        return $this->_query->andWhere($column.'>=:'.$minkey.' and '.$column.'<=:'.$maxkey, [
            ':'.$minkey => $min, ':'.$maxkey => $max
        ]);
    }

    /**
     * @param $column
     * @param $suff
     * @return string
     */
    protected function getColumnName($column, $suff)
    {
        return str_replace('.','_', $column).'_'.$suff;
    }

    /**
     * 获取排序的链接
     * @return string
     */
    public function getSortUrl()
    {
        $sortDefaultAtributes = [
            'height' => '体重',
            'grow' => '成长',
            'nuggets' => '掘金'
        ];
        $params = $this->getParams();
        $html = '';
        $orderParams = $this->getSortParams();
        foreach ($sortDefaultAtributes as $key => $item){
            if ($orderParams['order'] == $key) {
                if (empty($orderParams['sort'])) {
                    $sortScar = '-';
                    $sortIcon = ' ↑ ';
                    $sortParams = ['order' => $sortScar.$key];
                } elseif(($orderParams['sort'] == '-')) {
                    $sortIcon = ' ↓ ';
                    $sortScar = '';
                    $sortParams = [];
                }
                unset($params['order']);
                //$sortIcon = ($orderParams['sort'] == '-' || '') ? '↑' : '↓';
                $html .= Html::a($item.$sortIcon.' ', array_merge(['pet/list'], $params, $sortParams));
            } else {
                $html .= Html::a($item.' ', array_merge(['pet/list'], $params, ['order' => $key]));
            }
        }
        return $html;
    }

    /**
     * @return array
     */
    protected function getSortParams()
    {
        $order = Yii::$app->request->get('order');
        $sort = $orderColumn = '';
        if (!empty($order)) {
            $orderColumn = $order;
            if (strpos($order,'-') !== false) {
                $orderColumn = str_replace('-', '', $order);
                $sort = '-';
            }
        }
        return [
            'order' => $orderColumn,
            'sort' => $sort
        ];
    }

    /**
     * @return array|mixed
     */
    public function getParams()
    {
        $params = Yii::$app->request->post() ? Yii::$app->request->post() : Yii::$app->request->get();
        if (!empty($params)) {
            foreach ($params as $key => $val) {
                if (empty($val)) {
                    unset($params[$key]);
                }
            }
        }
        return $params;
    }
}